﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net;
using System.Globalization;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace ViewRunSim
{
    public partial class _Default : System.Web.UI.Page
    {
        System.Threading.Timer UItimer;
        public _Default() : base() 
        {
            if (LicenseSim.states.Count == 0)
            { 
                LicenseSim.states.Add(LicenseSim.TrialState.notrunning); 
            }
            //TimerCallback tcb = this.updateUI;
            //UItimer = new System.Threading.Timer(tcb, null, 0, 5 * 1000);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        
        delegate int RunSimDlg(double entryRate, double[] paperRates, double[] photoRates, double duration, string connString);

        delegate string MethodDelegate(int iCallTime, out int iExecThread);

        public void updateUI(object stateinfo) 
        {
            switch (LicenseSim.states[0]) 
            {
                case LicenseSim.TrialState.notrunning:
                    StatusLabel.Text = "Simulation not running";
                    StatusLabel.ForeColor = System.Drawing.Color.Black;
                    break;
                case LicenseSim.TrialState.up:
                    StatusLabel.Text = "Simulation up and running";
                    StatusLabel.ForeColor = System.Drawing.Color.Green;
                    break;
                case LicenseSim.TrialState.probation:
                    StatusLabel.Text="Simulation encountering errors";
                    StatusLabel.ForeColor = System.Drawing.Color.Yellow;
                    break;
                case LicenseSim.TrialState.failed:
                    StatusLabel.Text = "Simulation failed";
                    StatusLabel.ForeColor = System.Drawing.Color.Red;
                    break;
            
            }
        }

        protected void RunSimButton_Click(object sender, EventArgs e)
        {
            


            //StatusLabel.Text = "";
            try
            {
                int numSims = Int32.Parse(TextBox1.Text);
                if(numSims<0)
                {
                        throw new InvalidCastException();
                }
                
                System.Diagnostics.Trace.WriteLine("User requested simulation", "Information");
                for(int i=0;i<numSims;i++)
                {
                    RunSimDlg sim = new RunSimDlg(LicenseSim.RunSim);
                    sim.BeginInvoke(2.0, new double[] { 1.0, 2.0, 3.0, 3.5 }, new double[] { 1.0, 4.0 }, 100, ConfigurationManager.ConnectionStrings["licensesimDB"].ConnectionString, new AsyncCallback(CallbackMethod),sim);
                }
                LicenseSim.states[0] = LicenseSim.TrialState.up;
            }
            catch (Exception ex) 
            {
                StatusLabel.Text = "Bad input, only positive integers";
            }

        }
        static void CallbackMethod(IAsyncResult ar) 
        {
            RunSimDlg caller = (RunSimDlg)ar.AsyncState;
            int ret = caller.EndInvoke(ar);
            LicenseSim.states[0] = LicenseSim.TrialState.notrunning;
            System.Diagnostics.Trace.WriteLine("Finished Sim", "Information");
            
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            updateUI(null);
        }

    }
}
